package com.unohacha.qqj.fzdr.ui.activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qpg.superhttp.SuperHttp;
import com.qpg.superhttp.callback.SimpleCallBack;
import com.qpg.widget.common.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.bean.BaseBean;
import com.unohacha.qqj.fzdr.bean.CatData;
import com.unohacha.qqj.fzdr.bean.ImgBean;
import com.unohacha.qqj.fzdr.util.GlideImageLoaderForPicker;
import com.unohacha.qqj.fzdr.util.Prefer;
import com.unohacha.qqj.mylibrary.base.BaseActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的图库-面料分类
 */
public class CategoryActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener{

    @BindView(R.id.common_back)
    ImageView commonBack;
    @BindView(R.id.common_title)
    TextView commonTitle;
    @BindView(R.id.common_right_image)
    ImageView commonRightImage;
    //    @BindView(R.id.image_grid_layout)
//    MyGridView imageGridLayout;
    @BindView(R.id.mRefreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.recycleview)
    RecyclerView recycleview;
    private int a;
    private List<CatData> list;
    private List<CatData.Two> listTwo;
    private int fabricId;
    private String fabricName;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_supply_demand;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setText("分类");
        commonRightImage.setVisibility(View.INVISIBLE);
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoaderForPicker());   //设置图片加载器
        imagePicker.setShowCamera(true);  //显示拍照按钮
        imagePicker.setCrop(false);        //允许裁剪（单选才有效）
        imagePicker.setSaveRectangle(true); //是否按矩形区域保存
        imagePicker.setSelectLimit(1);    //选中数量限制
        imagePicker.setOutPutX(1000);//保存文件的宽度。单位像素
        imagePicker.setOutPutY(1000);//保存文件的高度。单位像素
        list = new ArrayList<>();
        commonRightImage.setVisibility(View.INVISIBLE);
//        imageGridLayout.setOnItemClickListener(this);
        WindowManager manager = getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        a = outMetrics.widthPixels - 50;
        GridLayoutManager layoutManage = new GridLayoutManager(this, 4);
        recycleview.setLayoutManager(layoutManage);
        getData();

    }

    private class MyAdapter extends BaseQuickAdapter<CatData.Two, BaseViewHolder> {

        public MyAdapter(int layoutResId, @Nullable List<CatData.Two> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, CatData.Two item) {
            ImageView imageView = (ImageView) helper.getView(R.id.supply_item_type_image);
            imageView.setMinimumWidth(a / 4);
            imageView.setMinimumHeight(a / 4);
            Glide.with(CategoryActivity.this)
                    .load(item.getPic())
             //       .error(R.mipmap.ic_default_photo)
                    .override(a / 4, a / 4)
                    .into(imageView);
            helper.setText(R.id.supply_item_type_text, item.getName());
        }
    }

    @OnClick(R.id.common_back)
    public void onViewClicked() {
        finish();
    }
    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        fabricId=listTwo.get(position).getId();
        fabricName=listTwo.get(position).getName();
        Intent intent = new Intent(this, ImgUploadActivity.class);
        intent.putExtra("fabricName",fabricName);
        intent.putExtra("fabricId",fabricId+"");
        startActivity(intent);

    }
    private void getData(){
        showLoading();
        SuperHttp.post("api/supplyinfo/supplyCate")
                .addParam("token",Prefer.getInstance().getToken())
                .addParam("typeid","2")
                .request(new SimpleCallBack<BaseBean<List<CatData>>>() {
                    @Override
                    public void onSuccess(BaseBean<List<CatData>> data) {
                        if(data.getCode()==0){
                            ToastUtil.showCustomToast("暂无数据");
                            return;
                        }
                        list.clear();
                        list.addAll(data.getData());
                        for(int i=0;i<list.size();i++){
                            if(list.get(i).getId()==1){
                                listTwo=data.getData().get(i).getTwo();
                                MyAdapter adapter = new MyAdapter(R.layout.activity_supply_demand_grid_item, data.getData().get(i).getTwo());
                                adapter.setOnItemClickListener(CategoryActivity.this);
                                recycleview.setAdapter(adapter);
                                if (mRefreshLayout.isRefreshing()) {
                                    mRefreshLayout.finishRefresh();
                                }
                            }
                        }

                        hideLoading();
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        hideLoading();
                    }
                });
    }
}
